/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.actions.transition;

import com.android1.amarena2d.actions.base.Action;
import com.android1.amarena2d.commons.Callback;
import com.android1.amarena2d.nodes.behavior.ActionTarget;
import com.badlogic.gdx.math.Vector2;

public abstract class Transition {


    protected ActionTarget inTarget;
    protected ActionTarget outTarget;

    protected Action inAction;
    protected Action outAction;

    protected final Vector2 posStartIn = new Vector2();
    protected final Vector2 posStartOut = new Vector2();

    public abstract Action getInAction(ActionTarget inTarget);

    public abstract Action getOutAction(ActionTarget outTarget);

    public void play(ActionTarget inTarget, ActionTarget outTarget) {

        if (isRunning()) {
            stop();
        }
        posStartIn.set(inTarget.getX(), inTarget.getY());
        posStartOut.set(outTarget.getX(), outTarget.getY());

        this.inTarget = inTarget;
        this.outTarget = outTarget;

        inTarget.action().run(inAction = getInAction(inTarget));
        inTarget.action().resumeAll();
        outTarget.action().run(outAction = getOutAction(outTarget));
        outTarget.action().resumeAll();
    }


    public void stop() {
        reset();
        if (inTarget != null && inAction != null)
            inTarget.action().stop(inAction);
        if (outTarget != null && outAction != null)
            outTarget.action().stop(outAction);
        inTarget = null;
        outTarget = null;

    }

    protected void reset() {
        inTarget.setXY(posStartIn.x, posStartIn.y);
        outTarget.setXY(posStartIn.x, posStartIn.y);
    }


    public boolean isRunning() {
        return (inAction != null && inAction.isRunning()) || (outAction != null && outAction.isRunning());
    }

    public boolean isDone() {
        return inAction != null && outAction != null && inAction.isDone() && outAction.isDone();
    }

    public void onFinished(final TransitionCallback transitionCallback) {
        if (transitionCallback != null) {
            inAction.setOnStopCallback(new Callback<ActionTarget>() {
                @Override
                public void on(ActionTarget element) {
                    transitionCallback.on(inTarget, outTarget);
                }
            });
        } else {
            inAction.setOnStopCallback(null);
        }
    }

    public static interface TransitionCallback {
        public void on(ActionTarget inTarget, ActionTarget outTarget);
    }

}
